<?php

namespace Shopadmin\Controller;

use Think\Controller;

class AlipayController extends Controller
{
    public function index()
    {
        vendor('Alipay.AopSdk');
        vendor('Alipay.aop.AopClient');
        //构造参数
        $conf = C("alipay_conf");
        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $conf['appId'];
        $aop->rsaPrivateKey = $conf['rsaPrivateKey'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';
        $request = new \AlipayTradePagePayRequest ();
        $request->setReturnUrl('http://shouhou.xunshengkj.com/Shopadmin/Ms/ms_buy_success');
        $request->setNotifyUrl('http://shouhou.xunshengkj.com/Shopadmin/Alipay/notify_url');

        //短信套餐信息
        $id = I("id", "");
        $info = M("package")->where("id=$id")->find();
        $product_code = "FAST_INSTANT_TRADE_PAY";
        $out_trade_no = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $subject = "短信充值";
        $total_amount = $info['money'];
        $body = "";
        $request->setBizContent('{"product_code":"' . $product_code . '","out_trade_no":"' . $out_trade_no . '","subject":"' . $subject . '","total_amount":"' . $total_amount . '","body":"' . $body . '"}');
        //请求
        $result = $aop->pageExecute($request);
        //插入充值记录
        $data["shop_id"] = session("shopid");
        $data["money"] = $info["money"];
        $data["count"] = $info["count"];
        $data["order_sn"] = $out_trade_no;
        $data["status"] = 0;
        $data["mothod"] = 1;
        $data["add_time"] = time();
        M("ms_order")->add($data);

        //输出
        echo $result;
    }

    /**
     * 短信支付回调
     */
    public function notify_url()
    {
        $data = $_POST;
       if ($data['trade_status'] == 'TRADE_SUCCESS' || $data['trade_status'] == 'TRADE_FINISHED') {
            vendor('Alipay.AopSdk');
            $conf = C("alipay_conf");
            $aop = new \AopClient();
            $aop->alipayrsaPublicKey = $conf['alipayPublicKey'];
           $res = $aop->rsaCheckV1($data, '', 'RSA2');
            if ($res) {
                $recharge_db = M('shop_recharge_order');
                $shop_db = M('shop');
                $order_where['order_number'] = $data['out_trade_no'];//订单号
                $order_info = $recharge_db->where($order_where)->find();
                $recharge_db->where($order_where)->setField("status", 1);
                $shop_id=$order_info['shop_id'];
                $disinfo=$shop_db->where(array('id'=>$shop_id))->find();
                $discount=$disinfo['rate']/100;
                $discount = 1 - $discount;
                $shop_money = sprintf("%.2f",$order_info['money'] * $discount);//四舍五入保留两位小数
                $order_info['shop_money'] = $shop_money;
                $recharge_db->where($order_where)->setField("discount", $discount);      //折扣
                $recharge_db->where($order_where)->setField("pay_type", 1);    //支付方式-支付宝支付
                $recharge_db->where($order_where)->setField("status", 1); //订单支付状态
                $recharge_db->where($order_where)->setField("paytime", time());//支付时间
                $recharge_db->where($order_where)->setField("shop_money", $shop_money);//最终给商户的钱
                $shop_db->where("id=" . $order_info['shop_id'])->setInc('money', $shop_money);//修改商家账户余额
                die("success");
            }
        }
    }


    public function notify_return()
    {
        $url = "/shopadmin/Login/info";
        $this->redirect($url);
    }

}